import 'package:bili_caricature/base/base_state.dart';
import 'package:bili_caricature/models/handle_state.dart';
import 'package:bloc/bloc.dart';
import '../../../models/response_model.dart';
import '../../../net/http_config.dart';
import '../../../net/ltt_https.dart';
import '../../../widgets/easy_loading.dart';
import '../../bloc/model/cartoon_model_entity.dart';
import '../../bloc/model/cartoon_recommend_entity.dart';
import '../../bloc/model/cartoon_series_entity.dart';
import 'state.dart';

class BiliMineCubit extends Cubit<BiliMineState> {
  BiliMineCubit() : super(BiliMineState().init());

  /// 并发请求全部数据
  getAllData() async {
    List<dynamic> list =
        await Future.wait<dynamic>([getMainData(), getSeriesData(), getRecommendData()]);
    XsEasyLoading.dismiss();
    state.mainModel = list[0];
    state.seriesList = list[1];
    state.recommendList = list[2];
    state.isRefreshMainView = true;
    state.netState = NetState.dataSuccessState;
    emit(state.clone());
  }

  /// 请求主数据
  getMainData() async {
    XsEasyLoading.showLoading();
    ResponseModel? responseMode = await LttHttp().request<CartoonModelData>(
        'https://run.mocky.io/v3/315de364-a765-40e1-8383-f36d3ffe5bdd',
        method: HttpConfig.get);
    return responseMode.data;
  }

  /// 请求系列数据
  getSeriesData() async {
    ResponseModel? responseModel = await LttHttp().request<CartoonSeriesData>(
        'https://run.mocky.io/v3/c1fecbc3-296f-44c4-970c-5861970cc11b',
        method: HttpConfig.get);
    CartoonSeriesData cartoonSeriesData = responseModel.data;
    return cartoonSeriesData.seriesComics;
  }

  /// 请求推荐数据
  getRecommendData() async {
    ResponseModel? responseModel = await LttHttp().request<CartoonRecommendData>(
        'https://run.mocky.io/v3/7b0096eb-a1ea-4f3c-8273-e6e700a01128',
        method: HttpConfig.get);
    CartoonRecommendData cartoonRecommendData = responseModel.data;
    return cartoonRecommendData.infos;
  }

  /// 改变透明度
  void changeAppBarAlphaEvent(double alpha, double newOffset) {
    var newStates = state.clone();
    newStates.isRefreshMainView = false;
    newStates.appBarAlpha = alpha;
    newStates.offset = newOffset;
    emit(newStates);
  }
}
